﻿unit UserFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvFormPlacement, JvComponentBase, JvAppStorage,
    cxGridExportLink,
  dxRibbonForm,
  JvAppIniStorage, JvXPBar, JvExControls, JvXPCore, JvXPContainer, GridsEh,
  DBGridEh, ComCtrls, JvExComCtrls, JvComCtrls, ExtCtrls, JvExExtCtrls,
  JvExtComponent, JvSplit, DB, JvXPButtons, Grids, DBGrids, FIBSQLMonitor,
  StdCtrls, CheckLst, DBCtrls, pFIBDataSet, dxSkinsCore, dxSkinMcSkin,
  dxSkinSeven, dxSkinsdxRibbonPainter, cxClasses, dxRibbon, cxControls,
  cxGraphics, cxLookAndFeels, cxLookAndFeelPainters, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver;

type
  TUserForm = class(Tdxcustomribbonform)
    Panel1: TPanel;
    Grid: TDBGridEh;
    DataSource: TDataSource;
    Grants: TCheckListBox;
    JvFormStorage1: TJvFormStorage;
    Storage: TJvAppIniFileStorage;
    DBNavigator1: TDBNavigator;
    GroupBox2: TGroupBox;
    Shb: TComboBox;
    RibbonTab1: TdxRibbonTab;
    Ribbon: TdxRibbon;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure GrantsClickCheck(Sender: TObject);
    procedure DataSourceDataChange(Sender: TObject; Field: TField);
    procedure ShbChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    procedure SetCheck(S: string);
    function GetCheck: string;
  public
    { Public declarations }
  end;

var
  UserForm: TUserForm;

implementation
 uses  cmUtils, DModule;

{$R *.dfm}


procedure TUserForm.DataSourceDataChange(Sender: TObject; Field: TField);
begin
 SetCheck(DataSource.DataSet.FieldByName('Profiles').AsString);
 Grants.Enabled:=(DataSource.DataSet.State in [dsEdit, dsInsert]);
end;

procedure TUserForm.FormActivate(Sender: TObject);
begin
  SetTabActive('spr',True);
end;

procedure TUserForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 RemoveBtn(Name);
 Action:=caFree;
end;

procedure TUserForm.FormCreate(Sender: TObject);
begin
 //InitGrids(Self);
 dm1.FDS_Users.CloseOpen(false);
 AddButton(self);
end;

function TUserForm.GetCheck: string;
 var i: integer;
begin
 Result:='';
 for i:=0 to Grants.Items.Count - 1 do
  if Grants.Checked[i] then Result:=Result+'1'
                       else Result:=Result+'0';
end;

procedure TUserForm.GrantsClickCheck(Sender: TObject);
 var DS: TDataSet;
begin
 DS:=DataSource.DataSet;
 if not DS.IsEmpty then
   begin
    if not (DS.State in [dsEdit, dsInsert]) then Exit;
    DS.FieldByName('Profiles').AsString:=GetCheck;
   end;
end;

procedure TUserForm.SetCheck(S: string);
 var i: integer;
begin
 for i:=0 to Grants.Items.Count - 1 do
  if Copy(S,i+1,1)='1' then Grants.Checked[i]:=true
                       else Grants.Checked[i]:=false;
end;

procedure TUserForm.ShbChange(Sender: TObject);
 var S: string;
     i: integer;
     DS1: TpFIBDataSet;
begin
 DS1:=dm1.FDS_Users;
 DS1.Close;
 DS1.CancelConditions;
 DS1.Conditions.Clear;
 if Shb.ItemIndex>0 then
   begin
    S:='';
    for i:=2 to Shb.ItemIndex do S:=S+'_';
    S:='Profiles LIKE '#39+S+'1%'#39;
    DS1.Conditions.AddCondition('Shb',S, true);
   end;
 DS1.ApplyConditions(true);
end;

end.
